
package com.idealighter.game.games.errenniuniu.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.ErrenniuniuRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.errenniuniu.ErRenNiuNiuPlayer;
import com.idealighter.game.games.errenniuniu.message.ReqExchangeChipsMsg;
import com.idealighter.game.games.errenniuniu.message.ReqReadyMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.common.PlayerPosition;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(502212)
public class ResEnterTableHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResEnterTableHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    // ResEnterTableMsg msg = (ResEnterTableMsg)message;
    player.position = PlayerPosition.TABLE;
    LOG.info("[二人牛牛]玩家[{}][{}]进入桌子[{}]成功", player.userName, player.playerId,
        player.table(player.playerId).getId());
    // 随机延时3到5秒兑换筹码和准备
    player.schedule(() -> {
      ErrenniuniuRoomDomain roomDomain = ((ErRenNiuNiuPlayer) player).roomDomain;

      MemInfo member = player.member(player.playerId);

      if (player.playerInfo.getGold() <= 0) {
        if (member.getChips() < roomDomain.getMinOne() * 8) {
          player.delayLogout();
        }
      } else if (member.getChips() < roomDomain.getMinOne() * 8) {
        exchangeChips(player);
        player.sendMsg(new ReqReadyMsg());
      }
    }, RandCodeUtil.random(3, 5), TimeUnit.SECONDS);
  }

  /**
   * 进入桌子后兑换全部金币为筹码,然后马上准备.
   * 
   * @param player . .
   */
  private void exchangeChips(Player player) {
    ReqExchangeChipsMsg exchangeChipsMsg = new ReqExchangeChipsMsg();
    exchangeChipsMsg.setGold((int) player.playerInfo.getGold());
    player.sendMsg(exchangeChipsMsg);
  }
}
